﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BusinessPlatformTool.BusinessObjectStructure;
using System.IO;
using System.Text.RegularExpressions;
using BusinessPlatformTool.Enumerator;

namespace BusinessPlatformTool.CodeFiles
{
    internal static class CreateWinFormFiles
    {
        public static void CreateFiles(IBusinessObject BusObj, string Path)
        {
            #region 创建对象的文件夹
            var NameSpacePath = Path + "\\" + BusObj.NameSpace;
            Directory.CreateDirectory(NameSpacePath);
            #endregion
            var TempPath = System.Windows.Forms.Application.StartupPath + "\\Template\\CodeStructure\\WinForm\\";
            CreateFormDesignerFile(BusObj, NameSpacePath, TempPath);
            CreateFormFile(BusObj, NameSpacePath, TempPath);
        }
        /// <summary>
        /// 创建窗体的设计文件
        /// </summary>
        /// <param name="BusObj"></param>
        /// <param name="NameSpacePath"></param>
        /// <param name="Path"></param>
        private static void CreateFormDesignerFile(IBusinessObject BusObj, string NameSpacePath, string Path)
        {
            string Note = string.Empty;
            //模板内容
            Note = File.OpenText(Path + "WinForm.Designer.txt").ReadToEnd();
            #region 固定值
            //命名空间
            Note = Note.Replace("%Namespace%", BusObj.NameSpace);
            //类描述
            Note = Note.Replace("%ClassDescription%", BusObj.ObjectName);
            //类名称
            Note = Note.Replace("%ClassName%", BusObj.ObjectCode);
            #endregion
            #region 单据Header
            //获取对象中的主表
            var table = BusObj.Lines.GetPrimaryTable();
            IDictionary<string, myControl> Controls = new Dictionary<string, myControl>();
            myControl control;
            #region 控件声明
            string ControlDeclareString = string.Empty;
            foreach (var field in table.Lines)
            {
                string ControlName = string.Empty;
                string ControlType = string.Empty;
                if (field.ControlType != Enumerator.BOControlType.WinFormControlType.CheckBox &&
                    field.ControlType != Enumerator.BOControlType.WinFormControlType.DataGridView)
                {
                    ControlName = "lbl" + field.PropertyName.ToString();
                    ControlType = "System.Windows.Forms.Label";
                    ControlDeclareString += "\r\n       private " + ControlType + " " + ControlName + ";";
                    control = new myControl();
                    control.Name = ControlName;
                    control.Type = BOControlType.WinFormControlType.Label;
                    control.ControlTypeString = ControlType;
                    control.Description = field.Description;
                    control.UnionControl = field.PropertyName.ToString();
                    Controls.Add(ControlName, control);
                }
                ControlName = field.PropertyName.ToString();
                ControlType = GetObjectString(field);
                ControlDeclareString += "\r\n       private " + ControlType + " " + field.PropertyName.ToString() + ";";
                control = new myControl();
                control.Name = ControlName;
                control.Type = field.ControlType;
                control.ControlTypeString = ControlType;
                control.PropertyName = field.PropertyName;
                control.Description = field.Description;
                if (field.ControlType == BOControlType.WinFormControlType.TextBox)
                {
                    if (field.DbType == DbType.Ntext || field.DbType == DbType.Text)
                        control.Multiline = true;
                }
                Controls.Add(ControlName, control);
            }
            Note = Note.Replace("%ControlsDeclare%", ControlDeclareString + "\r\n%ControlsDeclare%");
            #endregion
            #region 控件实例化
            string ControlInstanceString = string.Empty;
            foreach (var item in Controls)
            {
                control = item.Value;
                ControlInstanceString += "this." + control.Name + " = new " + control.ControlTypeString + "();\r\n            ";
            }
            Note = Note.Replace("%ControlsInstance%", ControlInstanceString + "\r\n%ControlsInstance%");
            #endregion
            #region 控件初始化
            #region 参考
            /*
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(24, 46);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "订单编码";  
             * 
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(497, 99);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(72, 16);
            this.checkBox1.TabIndex = 6;
            this.checkBox1.Text = "部分交货";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.checkBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "IsPartDelivery", true));
             * 
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(83, 16);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 21);
            this.textBox1.TabIndex = 10;
            this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "DocumentEntry", true));
             * 
            // 
            // comboBox1
            // 
            this.comboBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.bindingSource1, "DocumentStatus", true));
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(448, 16);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 20);
            this.comboBox1.TabIndex = 12;
             * 
            // 
            // dateTimePicker1
            // 
            this.dateTimePicker1.DataBindings.Add(new System.Windows.Forms.Binding("Value", this.bindingSource1, "DocDate", true));
            this.dateTimePicker1.Location = new System.Drawing.Point(449, 43);
            this.dateTimePicker1.Name = "dateTimePicker1";
            this.dateTimePicker1.Size = new System.Drawing.Size(120, 21);
            this.dateTimePicker1.TabIndex = 13;

             */
            #endregion
            string ControlInitialize = string.Empty;
            int n = 0;
            foreach (var item in Controls)
            {
                control = item.Value;
                #region 注释
                ControlInitialize += "//\r\n            ";
                ControlInitialize += "// " + control.Name + "\r\n            ";
                ControlInitialize += "//\r\n            ";
                #endregion
                #region
                ControlInitialize += "this." + control.Name + ".Name = \"" + control.Name + "\";\r\n            ";
                //在label和checkbox控件中才会使用此属性
                if (control.Type == BOControlType.WinFormControlType.Label || control.Type == BOControlType.WinFormControlType.CheckBox)
                {
                    ControlInitialize += "this." + control.Name + ".AutoSize = true;\r\n            ";
                    ControlInitialize += "this." + control.Name + ".Text = \"" + control.Description + "\";\r\n            ";
                }
                if (control.Type == BOControlType.WinFormControlType.CheckBox)
                    ControlInitialize += "this." + control.Name + ".UseVisualStyleBackColor = true;\r\n            ";
                if (control.Type == BOControlType.WinFormControlType.ComboBox)
                    ControlInitialize += "this." + control.Name + ".FormattingEnabled = true;\r\n            ";
                if (control.Type == BOControlType.WinFormControlType.TextBox && control.Multiline)
                    ControlInitialize += "this." + control.Name + ".Multiline = true;\r\n            ";
                #endregion
                #region 绑定
                //绑定
                if (control.Type != BOControlType.WinFormControlType.Label)
                {
                    if (control.Type == BOControlType.WinFormControlType.DateTimePicker)
                        ControlInitialize += "this." + control.Name + ".DataBindings.Add(new System.Windows.Forms.Binding(\"Value\", this.bindingSource, \"" + control.PropertyName + "\", true));\r\n            ";
                    else
                        ControlInitialize += "this." + control.Name + ".DataBindings.Add(new System.Windows.Forms.Binding(\"Text\", this.bindingSource, \"" + control.PropertyName + "\", true));\r\n            ";
                }
                #endregion
                if (control.Type == BOControlType.WinFormControlType.TextBox ||
                    control.Type == BOControlType.WinFormControlType.ComboBox ||
                    control.Type == BOControlType.WinFormControlType.DateTimePicker)
                {
                    myControl UnionControl = Controls.First(key => key.Value.UnionControl == control.Name).Value;
                    control.LocationX = UnionControl.LocationX + 60;
                    control.LocationY = UnionControl.LocationY;
                    ControlInitialize += "this." + control.Name + ".Location = new System.Drawing.Point(" + control.LocationX + ", " + (control.LocationY - 3).ToString() + ");\r\n            ";
                    ControlInitialize += "this." + control.Name + ".Size = new System.Drawing.Size(110, 21);\r\n            ";
                }
                else
                {
                    control.LocationX = 24;
                    control.LocationY = 19 + (27 * n);
                    ControlInitialize += "this." + control.Name + ".Location = new System.Drawing.Point(" + control.LocationX + ", " + control.LocationY + ");\r\n            ";
                    n++;
                }
                ControlInitialize += "this." + control.Name + ".TabIndex = 1;\r\n            ";
            }
            Note = Note.Replace("%ControlInitialize%", ControlInitialize + "\r\n%ControlInitialize%");
            #endregion
            #region 注册控件
            string ControlRegister = string.Empty;
            foreach (var item in Controls)
            {
                control = item.Value;
                ControlRegister += "this.Controls.Add(this." + control.Name + ");\r\n            ";
            }
            Note = Note.Replace("%ControlRegister%", ControlRegister + "\r\n%ControlRegister%");
            #endregion
            #endregion
            #region 单据Lines
            IList<ITable> Tables = BusObj.Lines.GetChildTable();
            IList<DataControl> myDataControls = default(IList<DataControl>);
            //两个子表及以上的行则添加页签
            if (Tables.Count > 1)
            {
                myDataControls = new List<DataControl>();
                #region 声明页签控件
                ControlDeclareString = "\r\n       private System.Windows.Forms.TabControl myTabControl;";
                for (int i = 1; i <= Tables.Count; i++)
                {
                    var myDataControl = new DataControl();
                    myDataControl.ControlName = "tabPage" + i;
                    myDataControl.ClassName = Tables[i].ClassName;
                    myDataControl.Description = Tables[i].Description;
                    myDataControls.Add(myDataControl);
                    ControlDeclareString += "\r\n       private System.Windows.Forms.TabPage tabPage" + i + ";";
                }
                Note = Note.Replace("%ControlsDeclare%", ControlDeclareString + "\r\n%ControlsDeclare%");
                #endregion
                #region 控件实例化
                /*
                this.myTabControl = new System.Windows.Forms.TabControl();
                this.tabPage1 = new System.Windows.Forms.TabPage();
                this.tabPage2 = new System.Windows.Forms.TabPage();
                this.myTabControl.SuspendLayout();
                 */
                ControlInstanceString = "this.myTabControl = new System.Windows.Forms.TabControl();\r\n            ";
                ControlInstanceString += "this.myTabControl.SuspendLayout();\r\n            ";
                foreach (var item in myDataControls)
                {
                    ControlInstanceString += "this." + item.ControlName + " = new System.Windows.Forms.TabPage();\r\n            ";
                }
                Note = Note.Replace("%ControlsInstance%", ControlInstanceString + "\r\n%ControlsInstance%");
                #endregion
                #region 控件初始化
                /*
                // 
                // tabControl1
                // 
                this.tabControl1.Location = new System.Drawing.Point(200, 12);
                this.tabControl1.Name = "tabControl1";
                this.tabControl1.SelectedIndex = 0;
                this.tabControl1.Size = new System.Drawing.Size(389, 322);
                this.tabControl1.TabIndex = 2;
                this.tabControl1.Controls.Add(this.tabPage1);
                // 
                // tabPage1
                // 
                this.tabPage1.Location = new System.Drawing.Point(4, 22);
                this.tabPage1.Name = "tabPage1";
                this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
                this.tabPage1.Size = new System.Drawing.Size(381, 296);
                this.tabPage1.TabIndex = 0;
                this.tabPage1.Text = "tabPage1";
                this.tabPage1.UseVisualStyleBackColor = true;
                */
                ControlInitialize = "// \r\n            ";
                ControlInitialize += "// myTabControl\r\n            ";
                ControlInitialize += "// \r\n            ";
                ControlInitialize += "this.myTabControl.Location = new System.Drawing.Point(200, 12);\r\n            ";
                ControlInitialize += "this.myTabControl.Name = \"myTabControl\";\r\n            ";
                ControlInitialize += "this.myTabControl.SelectedIndex = 0;\r\n            ";
                ControlInitialize += "this.myTabControl.Size = new System.Drawing.Size(389, 322);\r\n            ";
                ControlInitialize += "this.myTabControl.TabIndex = 2;\r\n            ";
                foreach (var item in myDataControls)
                {
                    ControlInitialize += "this.myTabControl.Controls.Add(this." + item.ControlName + ");\r\n            ";
                    ControlInitialize += "// \r\n            ";
                    ControlInitialize += "// " + item.ControlName + "\r\n            ";
                    ControlInitialize += "// \r\n            ";
                    ControlInitialize += "this." + item.ControlName + ".Location = new System.Drawing.Point(4, 22);\r\n            ";
                    ControlInitialize += "this." + item.ControlName + ".Name = \"" + item.ControlName + "\";\r\n            ";
                    ControlInitialize += "this." + item.ControlName + ".Padding = new System.Windows.Forms.Padding(3);\r\n            ";
                    ControlInitialize += "this." + item.ControlName + ".Size = new System.Drawing.Size(381, 296);\r\n            ";
                    ControlInitialize += "this." + item.ControlName + ".TabIndex = 0;\r\n            ";
                    ControlInitialize += "this." + item.ControlName + ".Text = \"" + item.Description + "\";\r\n            ";
                    ControlInitialize += "this." + item.ControlName + ".UseVisualStyleBackColor = true;\r\n            ";
                }
                Note = Note.Replace("%ControlInitialize%", ControlInitialize + "\r\n%ControlInitialize%");
                #endregion
                #region 注册控件
                /*
                 this.Controls.Add(this.tabControl1);
                 this.tabControl1.ResumeLayout(false);
                 */
                ControlRegister = "this.Controls.Add(this.myTabControl);\r\n            ";
                ControlRegister += "this.myTabControl.ResumeLayout(false);";
                Note = Note.Replace("%ControlRegister%", ControlRegister + "\r\n%ControlRegister%");
                #endregion
            }
            //只有一个子表
            if (myDataControls == null)
            {
                Note = AddDataGridView(BusObj.Lines.GetPrimaryTable(), Note);
            }
            else
            {
                #region 多个子表
                foreach (var item in myDataControls)
                {
                    var mytable = Tables.First(k => k.ClassName == item.ClassName);
                    Note = AddDataGridView(mytable, Note, item.ControlName);
                }
                #endregion
            }
            Note = Note.Replace("%ControlsDeclare%", "");
            Note = Note.Replace("%ControlsInstance%", "");
            Note = Note.Replace("%ControlInitialize%", "");
            Note = Note.Replace("%ControlRegister%", "");
            #endregion
            var sw = File.CreateText(NameSpacePath + "\\" + BusObj.ObjectCode + "WinForm.Designer.cs");
            sw.Write(Note);
            sw.Flush();
            sw.Close();
            sw.Dispose();

        }
        /// <summary>
        /// 创建窗体文件
        /// </summary>
        /// <param name="BusObj"></param>
        /// <param name="NameSpacePath"></param>
        /// <param name="Path"></param>
        private static void CreateFormFile(IBusinessObject BusObj, string NameSpacePath, string Path)
        {
            string Note = string.Empty;
            //模板内容
            Note = File.OpenText(Path + "WinForm.txt").ReadToEnd();
            #region 固定值
            //命名空间
            Note = Note.Replace("%Namespace%", BusObj.NameSpace);
            //类描述
            Note = Note.Replace("%ClassDescription%", BusObj.ObjectName);
            //类名称
            Note = Note.Replace("%ClassName%", BusObj.ObjectCode);
            //接口
            Note = Note.Replace("%InterfaceName%", "I" + BusObj.ObjectCode);
            #endregion
            var sw = File.CreateText(NameSpacePath + "\\" + BusObj.ObjectCode + "WinForm.cs");
            sw.Write(Note);
            sw.Flush();
            sw.Close();
            sw.Dispose();
        }
        /// <summary>
        /// 获取字段的控件类型
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private static string GetObjectString(IField field)
        {
            string ControlTypeString = string.Empty;
            switch (field.ControlType)
            {
                case Enumerator.BOControlType.WinFormControlType.TextBox:
                    ControlTypeString = "System.Windows.Forms.TextBox";
                    break;
                case Enumerator.BOControlType.WinFormControlType.CheckBox:
                    ControlTypeString = "System.Windows.Forms.CheckBox";
                    break;
                case Enumerator.BOControlType.WinFormControlType.ComboBox:
                    ControlTypeString = "System.Windows.Forms.ComboBox";
                    break;
                case Enumerator.BOControlType.WinFormControlType.DateTimePicker:
                    ControlTypeString = "System.Windows.Forms.DateTimePicker";
                    break;
                case Enumerator.BOControlType.WinFormControlType.DataGridView:
                    ControlTypeString = "System.Windows.Forms.DataGridView";
                    break;
            }
            return ControlTypeString;
        }
        private static string AddDataGridView(ITable table, string Note, string ControlName = null)
        {
            string ControlDeclare = "";
            string ControlInstance = "";
            string ControlInitialize = "";
            string ControlRegister = "";
            #region 单一子表
            #region 声明控件
            /*
                private System.Windows.Forms.DataGridView dataGridView1;
                private System.Windows.Forms.DataGridViewTextBoxColumn DocumentEntry;
                */
            ControlDeclare = "\r\n       private System.Windows.Forms.DataGridView " + table.ClassName + "DataGridView;";
            foreach (var field in table.Lines)
            {
                string ControlType = field.ControlType == BOControlType.WinFormControlType.DateTimePicker ? BOControlType.WinFormControlType.TextBox.ToString() : field.ControlType.ToString();
                ControlDeclare += "\r\n       private System.Windows.Forms.DataGridView" + ControlType + "Column " + table.ClassName + field.PropertyName + "; ";
            }
            Note = Note.Replace("%ControlsDeclare%", ControlDeclare + "%ControlsDeclare%");
            #endregion
            #region 控件实例化
            /*
                 this.dataGridView1 = new System.Windows.Forms.DataGridView();
                 ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                 this.DocumentEntry = new System.Windows.Forms.DataGridViewTextBoxColumn();
                 */
            ControlInstance = "            this." + table.ClassName + "DataGridView = new System.Windows.Forms.DataGridView();\r\n            ";
            ControlInstance += "((System.ComponentModel.ISupportInitialize)(this." + table.ClassName + "DataGridView)).BeginInit();\r\n            ";
            foreach (var field in table.Lines)
            {
                string ControlType = field.ControlType == BOControlType.WinFormControlType.DateTimePicker ? BOControlType.WinFormControlType.TextBox.ToString() : field.ControlType.ToString();
                ControlInstance += "this." + table.ClassName + field.PropertyName + " = new System.Windows.Forms.DataGridView" + ControlType + "Column();\r\n            ";
            }
            Note = Note.Replace("%ControlsInstance%", ControlInstance + "%ControlsInstance%");
            #endregion
            #region 控件初始化
            /*
                // 
                // dataGridView1
                // 
                this.dataGridView1.AutoGenerateColumns = false;
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                this.DocumentEntry,
                this.LineNumber,
                this.ItemCode,
                this.ItemName});
                this.dataGridView1.DataMember = "SalesOrderLines";
                this.dataGridView1.DataSource = this.bindingSource1;
                this.dataGridView1.Location = new System.Drawing.Point(12, 135);
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 23;
                this.dataGridView1.Size = new System.Drawing.Size(577, 215);
                this.dataGridView1.TabIndex = 7;
                 */
            ControlInitialize = "            // \r\n            ";
            ControlInitialize += "// " + table.ClassName + "DataGridView\r\n            ";
            ControlInitialize += "// \r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.AutoGenerateColumns = false;\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {\r\n            ";
            foreach (var field in table.Lines)
            {
                ControlInitialize += "this." + table.ClassName + field.PropertyName + ",\r\n            ";
            }
            ControlInitialize = ControlInitialize.Substring(0, ControlInitialize.LastIndexOf(',')) + "	});\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.DataMember = \"" + table.ClassName + "s\";\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.DataSource = this.bindingSource;\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.Location = new System.Drawing.Point(12, 135);\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.Name = \"" + table.ClassName + "DataGridView\";\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.RowTemplate.Height = 23;\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.Size = new System.Drawing.Size(577, 215);\r\n            ";
            ControlInitialize += "this." + table.ClassName + "DataGridView.TabIndex = 7;\r\n            ";
            /*
            // 
            // DocumentEntry
            // 
            this.DocumentEntry.DataPropertyName = "DocumentEntry";
            this.DocumentEntry.HeaderText = "DocumentEntry";
            this.DocumentEntry.Name = "DocumentEntry";
             */
            foreach (var field in table.Lines)
            {
                ControlInitialize += "// \r\n            ";
                ControlInitialize += "// " + table.ClassName + field.PropertyName + "\r\n            ";
                ControlInitialize += "// \r\n            ";
                ControlInitialize += "this." + table.ClassName + field.PropertyName + ".DataPropertyName = \"" + field.PropertyName + "\";\r\n            ";
                ControlInitialize += "this." + table.ClassName + field.PropertyName + ".HeaderText = \"" + field.Description + "\";\r\n            ";
                ControlInitialize += "this." + table.ClassName + field.PropertyName + ".Name = \"" + table.ClassName + field.PropertyName + "\";\r\n            ";
            }
            Note = Note.Replace("%ControlInitialize%", ControlInitialize + "%ControlInitialize%");
            #endregion
            #region 注册控件
            /*
                this.Controls.Add(this.dataGridView1);
             * this.tabPage1.Controls.Add(this.dataGridView1);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                 */
            if (ControlName == null)
                ControlRegister = "            this.Controls.Add(this." + table.ClassName + "DataGridView);\r\n            ";
            else
                ControlRegister = "            this." + ControlName + ".Controls.Add(this." + table.ClassName + "DataGridView);";
            ControlRegister += "((System.ComponentModel.ISupportInitialize)(this." + table.ClassName + "DataGridView)).EndInit();\r\n            ";
            Note = Note.Replace("%ControlRegister%", ControlRegister + "%ControlRegister%");
            #endregion
            #endregion
            return Note;
        }

        class myControl
        {
            public string Name;
            public BOControlType.WinFormControlType Type;
            public bool Multiline;
            public string ControlTypeString;
            public string Description;
            public string PropertyName;
            public string UnionControl;
            public int LocationX;
            public int LocationY;
            public int SizeX;
            public int SizeY;

        }
        class DataControl
        {
            public string ControlName;
            public string Description;
            public string ClassName;
        }
    }
}
